import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

plt.rcParams['font.sans-serif'] = ['DejaVu Sans']  # 中文字体（可选）
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['axes.unicode_minus'] = False
df=pd.read_csv('winequality_red.csv')
df2=df.groupby('quality').count().sort_values('alcohol',ascending=False)
df3=df2.alcohol
print('分布最多的3种品质是：',df3.head(3))
labels=df2.index
sizes=df2.alcohol
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
# 保证饼图是圆形的（而不是椭圆）
plt.axis('equal')
# 显示图形
plt.title('quality of wine')
plt.savefig('wine_quality.png')
plt.show()
#以下是整合到一张表中进行分析对比
plt.figure(figsize=(16,8))
list=['fixed acidity', 'volatile acidity', 'citric acid', 'free sulfur dioxide', 'total sulfur dioxide', 'sulphates']
for i , col in enumerate(list):
    #四行三列子图布局
    plt.subplot(2,3,i+1)
    df=df[df[col]>0]
    sns.histplot(np.log10(df[col]),kde=True,bins=30,color='blue')
    plt.title(f' {col} ')
    # plt.xscale('log')
    #添加x轴标签
    plt.xlabel(f'log({col})')
    plt.xticks(rotation=45, ha='right', fontsize=6)
#避免子图重叠
plt.tight_layout()
plt.savefig('loghis.png')
plt.show()
plt.figure(figsize=(12,6))
bins = 10**(np.linspace(-2, 2))
plt.hist(df['fixed acidity'], bins = bins, edgecolor = 'k', label = 'Fixed Acidity')
plt.hist(df['volatile acidity'], bins = bins, edgecolor = 'k', label = 'Volatile Acidity')
plt.hist(df['citric acid'], bins = bins, edgecolor = 'k', alpha = 0.8, label = 'Citric Acid')
plt.hist(df['free sulfur dioxide'], bins = bins, edgecolor = 'k', label = 'free sulfur dioxide')
plt.hist(df['total sulfur dioxide'], bins = bins, edgecolor = 'k', label = 'total sulfur dioxide')
plt.hist(df['sulphates'], bins = bins, edgecolor = 'k', label = 'sulphates')
plt.xscale('log')
plt.xlabel('Acid Concentration (g/dm^3)')
plt.ylabel('Frequency')
plt.title('Histogram of Acid Concentration')
plt.legend()
plt.tight_layout()
plt.savefig('Acid Concentration.png')
plt.show()
print('Figure 4')
df['total_acid']=df['fixed acidity']+df['volatile acidity']+df['citric acid']
log_x = np.log(df['total_acid'])
plt.hist(log_x,  edgecolor = 'k', label = 'total_acid')
# plt.xscale('log')
plt.xlabel('log(Acid Concentration)')
plt.ylabel('Frequency')
plt.title('Histogram of Acid Concentration')
plt.legend()
plt.tight_layout()
plt.savefig('total_Acid.png')
plt.show()
#甜度
df['type'] = df['residual sugar'].apply(lambda x: '干红' if x < 4 else ('半干' if 4 <= x < 12 else ('半甜' if 12 <= x < 45 else '全甜')))
df4=df.groupby('type').count().alcohol
df4.plot(kind='bar')
plt.savefig('sweetness.png')
plt.show()
#拓展部分：含硫量
plt.figure(figsize=(12,6))
plt.hist(df['free sulfur dioxide'], edgecolor = 'k', label = 'free sulfur dioxide')
plt.hist(df['total sulfur dioxide'], edgecolor = 'k', label = 'total sulfur dioxide')
plt.xlabel('Acid Concentration (g/dm^3)')
plt.ylabel('Frequency')
plt.title('Histogram of Acid Concentration')
plt.legend()
plt.tight_layout()
plt.savefig('sulfur dioxide.png')
plt.show()
#总含硫量
df['总含硫量']=df['free sulfur dioxide']+df['total sulfur dioxide']+df['sulphates']
plt.hist(df['总含硫量'], edgecolor = 'k', label = '总含硫量')
plt.xlabel('总含硫量')
plt.ylabel('Frequency')
plt.savefig('总含硫量.png')
plt.show()

